﻿(function (ekb, $, undefined) {
    ekb.loadCharacterShips = function (url, id) {
        this.post(url,
            JSON.stringify({ id: id }),
            this.showImage('topCharacterShipsLoading'),
            function (result) { ekb.appendText('div#topCharacterShipsContent', result.responseText, false); });
    };
    ekb.loadCharacterShipGroups = function(url, id) {
        this.post(url,
            JSON.stringify({ id: id }),
            this.showImage('topCharacterShipGroupsLoading'),
            function (result) { ekb.appendText('div#topCharacterShipGroups', result.responseText, false); });
    };
    ekb.loadCharacterWeapons = function (url, id) {
        this.post(url,
            JSON.stringify({ id: id }),
            this.showImage('topCharacterWeaponsLoading'),
            function (result) { ekb.appendText('div#topCharacterWeaponsContent', result.responseText, false); }
        );
    };
    ekb.loadActivity = function (url, id) {
        this.post(url,
            JSON.stringify({ id: id }),
            null,
            null,
            function (result) {
                if (result !== null && result !== undefined) {
                    drawActivityGraph(document.getElementById('entityActivity'), result, 0);
                }
            }
        );
    };
    ekb.loadCharacterContribution = function (url, id, corpId) {
        this.post(url,
            JSON.stringify({ id: id, corpId: corpId }),
            null,
            null,
            function (result) {
                if (result !== null && result !== undefined) {
                    var charKills = result.charKills ? result.charKills : 0;
                    var corpKills = result.corpKills ? result.corpKills : 0;
                    drawContributionPie(document.getElementById('contribution'), result, corpKills > 0 ? charKills / corpKills : 0.0);
                }
            }
        );
    };
    ekb.loadCorporationCharactersKillsMonth = function (url, year, month, corp) {
        this.post(url,
            JSON.stringify({ year: year, month: month, corp: corp }),
            this.showImage('topCharacterKillsMonthLoading'),
            function (result) { ekb.appendText('div#topCharacterKillsMonthContent', result.responseText, false); }
        );
    };
    ekb.loadCorporationCharactersLossesMonth = function (url, year, month, corp) {
        this.post(url,
        JSON.stringify({ year: year, month: month, corp: corp }),
        this.showImage('topCharacterLossesMonthLoading'),
        function (result) { ekb.appendText('div#topCharacterLossesMonthContent', result.responseText, false); }
    );
    };
    ekb.loadAllianceCorpKills = function (url,id, year, month) {
        this.post(url,
            JSON.stringify({ id: id, year: year, month: month }),
            this.showImage('topAllianceCorpKillsLoading'),
            function (result) { ekb.appendText('div#topAllianceCorpKillsContent', result.responseText, false); }
        );
    };
    ekb.loadAllianceCorpLosses = function (url, id, year, month) {
        this.post(url,
            JSON.stringify({ id: id, year: year, month: month }),
            this.showImage('topAllianceCorpLossesLoading'),
            function (result) { ekb.appendText('div#topAllianceCorpLossesContent', result.responseText, false); }
        );
    };
    ekb.loadCorporationContribution = function (url, corpId, allyId) {
        this.post(url,
            JSON.stringify({ corpId: corpId, allyId: allyId }),
            null,
            null,
            function (result) {
                if (result !== null && result !== undefined) {
                    var corpKills = result.corpKills ? result.corpKills : 0;
                    var allyKills = result.allyKills ? result.allyKills : 0;
                    drawContributionPie(document.getElementById('contribution'), result, allyKills > 0 ? corpKills / allyKills : 0.0);
                }
            }
        );
    };

    function drawContributionPie(canvas, source, percent) {
        if (canvas === undefined) return;

        var context = canvas.getContext("2d");
        context.clearRect(0, 0, canvas.width, canvas.height);

        context.globalCompositeOperation = 'source-over';
        context.lineWidth = 4;
        context.arc(canvas.width / 2, canvas.height / 2, canvas.width / 4, Math.PI * 2 * percent, Math.PI * 2);
        context.strokeStyle = '#2b380e';
        context.stroke();

        context.globalCompositeOperation = 'destination-over';
        context.lineWidth = 6;
        context.arc(canvas.width / 2, canvas.height / 2, canvas.width / 4, Math.PI * 0, Math.PI * 2 * percent);
        context.strokeStyle = '#8dbc34';
        context.stroke();

        var text = parseInt(percent * 100) + '%';
        context.lineWidth = 1;
        context.font = '9px sans-serif';
        var fonsize = 7;
        context.fillStyle = '#8dbc34';
        context.fillText(text, (canvas.width / 2) - (context.measureText(text).width / 2), (canvas.height / 2) + (fonsize / 2));
    }
    function drawActivityGraph(canvas, sources, percent) {
        if (canvas === undefined) return;

        if (percent > 1.0) {
            percent = 1.0;
        }
        var context = canvas.getContext("2d");
        context.clearRect(0, 0, canvas.width, canvas.height);
        context.font = '9px sans-serif';

        var margin = 1.5;
        var blockWidth = (canvas.width / 7) - margin;
        var maxKills = parseInt(sources.maxKills);
        var maxLosses = parseInt(sources.maxLosses);
        var fontSize = 9;

        var max = maxKills > maxLosses ? maxKills : maxLosses;

        for (var i = 0; i < 7; i++) {

            var kvalue = sources.kills[i] === undefined ? 0 : sources.kills[i].value * percent;
            var lvalue = sources.losses[i] === undefined ? 0 : sources.losses[i].value * percent;

            var kx = (margin * (i + 1)) + (blockWidth * i);
            var ky = (canvas.height / 2) - ((canvas.height / 2 - fontSize - margin) * (kvalue / max));
            var kh = (canvas.height / 2 - fontSize - margin) * (kvalue / max);
            var kw = blockWidth - margin;

            var lx = (margin * (i + 1)) + (blockWidth * i);
            var ly = canvas.height / 2 + margin;
            var lh = (canvas.height / 2 - (margin * 2) - fontSize) * (lvalue / max);
            var lw = kw;

            context.fillStyle = '#8DBC34';
            context.fillRect(kx, ky, kw, kh == 0 ? 1 : kh);

            context.fillStyle = '#FF0000';
            context.fillRect(lx, ly, lw, lh == 0 ? 1 : lh);

            if (percent == 1 && (lvalue > 0 || kvalue > 0)) {
                context.fillStyle = '#FFFFFF';

                if (kvalue > 0) {
                    context.fillText(kvalue, kx + ((kw - context.measureText(kvalue).width) / 2), ky - margin, blockWidth);
                }
                if (lvalue > 0) {
                    context.fillText(lvalue, lx + ((lw - context.measureText(lvalue).width) / 2), ly + lh + fontSize + margin, blockWidth);
                }
            }
        }
        if (percent < 1) {
            window.setTimeout(function () { drawActivityGraph(canvas, sources, percent + 0.1); }, 10);
        }
    }

} (window.evekillboard = window.evekillboard || {}, jQuery));

